﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LearningCompany_DAO
{
    public partial class Client
    {
        #region Méthodes
        public static Client CreateClient(string aRaisonSociale, string aReference, string aAdresse, string aCodePostal, string aVille, string aTelephone, string aEmail, string aUrlSiteWeb, SecteurActivite aSecteurActivite, Commercial aCommercial)
        {
            Client oClient = new Client();

            oClient.RaisonSociale = aRaisonSociale;
            oClient.Reference = aReference;
            oClient.MotDePasse = "achanger";
            oClient.Adresse = aAdresse;
            oClient.CodePostal = aCodePostal;
            oClient.Ville = aVille;
            oClient.Telephone = aTelephone;
            oClient.Email = aEmail;
            oClient.SecteurActivite = aSecteurActivite;
            oClient.Commercial = aCommercial;

            return oClient;
        }

        partial void OnCodePostalChanging(string value)
        {
            if (!Regex.Match(value, @"^\d{5}$").Success)
            {
                throw new Exception("Format du code postal '" + value + "' incorrect.");
            }
        }

        public static Client GetInstance(int aIdentifiant)
        {
            return (from oClient in ContexteDAO.ContexteDonnees.Client
                    where oClient.Identifiant == aIdentifiant
                    select oClient).FirstOrDefault();
        }

        public static Client GetInstance(string aReference, string aMotDePasse)
        {
            return (from oClient in ContexteDAO.ContexteDonnees.Client
                    where oClient.Reference == aReference
                        && oClient.MotDePasse == aMotDePasse
                    select oClient).FirstOrDefault();
        }

        public static Client GetInstance(string aReference)
        {
            return (from oClient in ContexteDAO.ContexteDonnees.Client
                    where oClient.Reference == aReference
                    select oClient).FirstOrDefault();
        }

        public static List<Client> GetListeInstances()
        {
            return (from oClient in ContexteDAO.ContexteDonnees.Client
                    select oClient).ToList();
        }

        public void DemanderInformations(Formation aFormation)
        {
            this.DemandesClients.Add(LearningCompany_DAO.DemandeClient.CreateDemandeClient(this, aFormation));
            ContexteDAO.Enregistrer();
        }

        public Stagiaire AjouterStagiaire(Civilite aCivilite, string aNom, string aPrenom, string aEmail, string aInfos)
        {
            Stagiaire oStagiaire;

            oStagiaire = new Stagiaire();
            oStagiaire.Civilite = aCivilite;
            oStagiaire.Nom = aNom;
            oStagiaire.Prenom = aPrenom;
            oStagiaire.Email = aEmail;

            return oStagiaire;
        }

        public List<Stagiaire> GetListeStagiaires()
        {
            return (from oStagiaire in ContexteDAO.ContexteDonnees.Stagiaire
                    where oStagiaire.Client.Identifiant == this.Identifiant
                    orderby oStagiaire.Nom, oStagiaire.Prenom
                    select oStagiaire).ToList();
        }

        public List<DemandeClient> GetListeDemandesInfosFormations()
        {
            return (from oDemandeClient in this.DemandesClients
                    orderby oDemandeClient.DateDemande descending
                    select oDemandeClient).ToList();
        }

        public List<DemandeClient> GetListeDemandesInfosFormationsATraiter()
        {
            return (from oDemandeClient in this.DemandesClients
                    where oDemandeClient.DateTraitement == null
                    orderby oDemandeClient.DateDemande descending
                    select oDemandeClient).ToList();
        }

        public List<SessionFormation> GetListeSessionsFormations()
        {
            return (from oStagiaire in this.Stagiaires
                    from oSessionFormation in oStagiaire.SessionsFormation
                    select oSessionFormation).Distinct().ToList();

        }
        #endregion
    }
}
